<?php
class AccountForm extends CFormModel{
	public $user_name;
	public $password;
	public $is_remember;
	public $email;
	private $_identity;
	

	public function rules(){
		return array(
			array('password,email','required'),
			array('user_name','required','on'=>'register'),
			array('is_remember','boolean','on'=>'login'),
			array('email','email','on'=>'register,login'),
			array('password','authenticate'),
		);
	}
	
	public function attributeLabels(){
		return array(
			'is_remember'=>'保持登录',
			'user_name'=>'昵称',
			'password'=>'密码',
			'email'=>'邮箱'
		);
	}

	public static function getIdentity(){
	}

	public function authenticate(){
/*
		$this->_identity=new UserIdentity($this->email,$this->password);
//		echo '--------login form autho----<br />';
		if(!$this->_identity->authenticate())
			$this->addError('password','错误的用户名和密码！');
	*/
	}

	public function login(){
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->email,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->is_remember ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else{
			return false;}
	}

	public function afterSave(){}
}